package com.study.business.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.study.business.user.domain.User;
import com.study.business.user.service.UserService;
import com.study.business.util.ResponseFailed;
import com.study.business.util.ResponseParam;
import com.study.business.util.ResponseSuccess;
import com.study.business.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/********************************************
 * @author: zhang
 * @Description: com.green.user.web_user.controller
 * @Date: 上午 12:54 2018/6/3 0003
 * @Modified By:
 ********************************************/
@RestController
@RequestMapping(value = "core")
@Slf4j
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    private final AtomicLong atomicLong = new AtomicLong(1L);
    @Autowired
    private UserService userService;

    @RequestMapping(value = "translate")
    public ResponseUtil translate(@RequestParam("word") String word) {
        LOGGER.debug("in translate");
        if (StringUtils.isEmpty(word)) {
            return new ResponseParam();
        }
        String result = userService.translate(word);
        if (StringUtils.isEmpty(result)) {
            return new ResponseFailed();
        }
        return new ResponseSuccess(result);
//        if (atomicLong.get() % 2 == 0) {
//            throw new RuntimeException("");
//        }
//        return new ResponseSuccess("");
    }

    @RequestMapping("users")
    public List<User> getAll() {
        return userService.getAll();
    }

    @RequestMapping(value = "user", method = RequestMethod.POST)
    public ResponseUtil addUser(@RequestBody User user) {
        try {
            boolean result = userService.addUser(user);
            log.info("添加用户:{}", JSONObject.toJSONString(user));
            return new ResponseSuccess(result);
        } catch (UnsupportedEncodingException e) {
            log.error("添加用户失败", e);
        } catch (NoSuchAlgorithmException e) {
            log.error("添加用户失败", e);
        }
        return new ResponseFailed();
    }

    @RequestMapping(value = "user/{id:.*}", method = RequestMethod.DELETE)
    public ResponseUtil deleteUser(@PathVariable String id) {
        return userService.deleteUser(id);
    }

    @RequestMapping(value = "user/{id:.*}", method = RequestMethod.PUT)
    public ResponseUtil updateUser(@PathVariable("id") int id, @RequestBody User user) {
        return userService.updateUser(id, user);
    }

    @RequestMapping(value = "user/login", method = RequestMethod.POST)
    public ResponseUtil login(@RequestBody User user) {
        return userService.checkUser(user);
    }

}
